
#include <sys/atomic.h>

uint32_t fetch_and_nand_4(volatile uint32_t *, uint32_t, ...)
    asm("__sync_fetch_and_nand_4");
uint32_t nand_and_fetch_4(volatile uint32_t *, uint32_t, ...)
    asm("__sync_nand_and_fetch_4");

uint32_t
fetch_and_nand_4(volatile uint32_t *addr, uint32_t val, ...)
{
	uint32_t old, new;

	do {
		old = *addr;
		new = ~(old & val);
	} while (atomic_cas_32(addr, old, new) != old);
	return old;
}

uint32_t
nand_and_fetch_4(volatile uint32_t *addr, uint32_t val, ...)
{
	uint32_t old, new;

	do {
		old = *addr;
		new = ~(old & val);
	} while (atomic_cas_32(addr, old, new) != old);
	return new;
}
